﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BUS;
using DTO;
using System.Drawing;

namespace TuyenDungViecLam.Admin
{
    public partial class EditRoleUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            IEnumerable dsRole = User_BUS.GetAllRole();


            CblAddRoleForUser.DataSource = dsRole;
            CblAddRoleForUser.DataTextField = "Name";
            CblAddRoleForUser.DataValueField = "RoleID";
            CblAddRoleForUser.DataBind();

            int userID = int.Parse(Request.QueryString["uid"].ToString());


            User_DTO user = User_BUS.GetUserInfomationByUserID(userID);
            txtUsername.Text = user.UserName;

            List<int> dsRoleUser = User_BUS.GetRoleUser(userID);

            for (int i = 0; i < CblAddRoleForUser.Items.Count; i++)
            {
                for (int j = 0; j < dsRoleUser.Count; j++)
                {
                    if (int.Parse(CblAddRoleForUser.Items[i].Value.ToString()) == dsRoleUser[j])
                    {
                        CblAddRoleForUser.Items[i].Selected = true;
                    }
                }

            }
        }

        protected void BtUpdateUser_Click(object sender, EventArgs e)
        {
            int userID = int.Parse(Request.QueryString["uid"].ToString());

            bool deleteResult = User_BUS.DeleteAllRoleUser(userID);

            if (deleteResult == true)
            {
                bool InsertResult = false;
                for (int i = 0; i < CblAddRoleForUser.Items.Count; i++)
                {

                    if (CblAddRoleForUser.Items[i].Selected == true)
                    {
                        InsertResult = User_BUS.InsertRoleUser(userID, int.Parse(CblAddRoleForUser.Items[i].Value.ToString()));

                        if (InsertResult == false)
                        {
                            break;
                        }
                        else
                        {
                            InsertResult = true;
                        }
                    }
                }

                if (InsertResult == true)
                {
                    divCapnhat.Visible = false;
                    divThongbao.Visible = true;
                    lbThongbao.Text = "Chỉnh sửa Role thành công";
                    lbThongbao.ForeColor = Color.Blue;
                }
                else
                {
                    divThongbao.Visible = true;
                    lbThongbao.Text = "Chỉnh sửa Role thất bại";
                    lbThongbao.ForeColor = Color.Red;
                }

            }
            else
            {
                divThongbao.Visible = true;
                lbThongbao.Text = "Chỉnh sửa Role thất bại";
                lbThongbao.ForeColor = Color.Red;
            }
        }
    }
}
